package com.ych.SDK.alibaba.smsSDK;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.ych.SDK.alibaba.smsSDK.config.AlibabaSmsConfig;
import org.springframework.stereotype.Component;

/**
 * Created with IntelliJ IDEA.
 * Author: Usopp.tsui
 * Date: 2020/12/14
 * Time: 14:51
 * Description:
 */
@Component
public class SmsUtil {
    public static void main(String[] args) {
        System.out.println(AlibabaSmsConfig.access_key_id);
//        sendSms("15982470213", 9858);
    }

    /**
     * 发送一条短信
     *
     * @param phoneNumbers 发送到的电话号码
     * @param validateCode 发送出去的验证码
     * @return 发送后的响应数据
     */
    public static boolean sendSms(String phoneNumbers, int validateCode) {
        DefaultProfile profile = DefaultProfile.getProfile(
                AlibabaSmsConfig.region_id,
                AlibabaSmsConfig.access_key_id,
                AlibabaSmsConfig.secret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain(AlibabaSmsConfig.domain);
        request.setVersion(AlibabaSmsConfig.version);
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", AlibabaSmsConfig.region_id);
        request.putQueryParameter("PhoneNumbers", phoneNumbers);
        request.putQueryParameter("SignName", AlibabaSmsConfig.sign_name);
        request.putQueryParameter("TemplateCode", AlibabaSmsConfig.template_code);
        String code = "{\"code\":\"" + validateCode + "\"}";
        request.putQueryParameter("TemplateParam", code);
        try {
            CommonResponse response = client.getCommonResponse(request);
//            return response.getData();
            return true;
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }
}
